package br.edu.ufcg.splab.util;

import static org.junit.Assert.assertTrue;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;

import org.junit.Test;

import br.edu.ufcg.splab.core.InterfaceGenerate;
import br.edu.ufcg.splab.core.InterfaceGraph;
import br.edu.ufcg.splab.core.graph.Tree;
import br.edu.ufcg.splab.exceptions.LTSBTException;
import br.edu.ufcg.splab.generator.ConvertGraphToTree;
import br.edu.ufcg.splab.generator.TestCaseGenerator;
import br.edu.ufcg.splab.generator.TestSuite;

public class FailuresIdentifierTest {

	@Test
	public void testGetFailures(){
		try{
			String graphFile = "./test-data/input/tgfFailureIdentifierTest.tgf";
			ReadTGF parser = new ReadTGF();			
			InterfaceGraph graph = parser.getGraph(graphFile);
			
			InterfaceGenerate generator = new TestCaseGenerator();
			ConvertGraphToTree convertsGraphToTree = new ConvertGraphToTree();
			Tree tree = convertsGraphToTree.converterGraph(graph);
			tree.expand(1);
			TestSuite testSuite = generator.generate(tree);		
			MapTestSuite mapTS = new MapTestSuite(testSuite);
			
			FailuresIdentifier failuresIdentifier = new FailuresIdentifier();
			TestSuite ts1 = failuresIdentifier.getFailures(testSuite, "f#g#h");

			assertTrue(!ts1.contains(mapTS.getTestCase(1)));
			assertTrue(ts1.contains(mapTS.getTestCase(2)));
			assertTrue(ts1.contains(mapTS.getTestCase(3)));
			assertTrue(!ts1.contains(mapTS.getTestCase(4)));
			assertTrue(ts1.contains(mapTS.getTestCase(5)));
			assertTrue(!ts1.contains(mapTS.getTestCase(6)));
			assertTrue(!ts1.contains(mapTS.getTestCase(7)));
			assertTrue(!ts1.contains(mapTS.getTestCase(8)));
			assertTrue(!ts1.contains(mapTS.getTestCase(9)));
			assertTrue(!ts1.contains(mapTS.getTestCase(10)));
			assertTrue(ts1.contains(mapTS.getTestCase(11)));
			assertTrue(!ts1.contains(mapTS.getTestCase(12)));
			assertTrue(!ts1.contains(mapTS.getTestCase(13)));
			
			TestSuite ts2 = failuresIdentifier.getFailures(testSuite, "i#g");

			assertTrue(!ts2.contains(mapTS.getTestCase(1)));
			assertTrue(ts2.contains(mapTS.getTestCase(2)));
			assertTrue(ts2.contains(mapTS.getTestCase(3)));
			assertTrue(!ts2.contains(mapTS.getTestCase(4)));
			assertTrue(!ts2.contains(mapTS.getTestCase(5)));
			assertTrue(!ts2.contains(mapTS.getTestCase(6)));
			assertTrue(ts2.contains(mapTS.getTestCase(7)));
			assertTrue(ts2.contains(mapTS.getTestCase(8)));
			assertTrue(ts2.contains(mapTS.getTestCase(9)));
			assertTrue(!ts2.contains(mapTS.getTestCase(10)));
			assertTrue(ts2.contains(mapTS.getTestCase(11)));
			assertTrue(!ts2.contains(mapTS.getTestCase(12)));
			assertTrue(!ts2.contains(mapTS.getTestCase(13)));
			
		}catch (LTSBTException e){
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}
	
	@Test
	public void testGetFailures2(){
		try{
			String fileInput = "./test-data/failures/test_failures_input.txt";
			String fileOutput = "./test-data/failures/test_failures_output.txt";
			String fileOutput_expected = "./test-data/failures/test_failures_output_expected.txt";
			String path = "./test-data/failures/";
			FailuresIdentifier failuresIdentifier = new FailuresIdentifier();
			failuresIdentifier.getFailuresFromGraph(path, fileInput, fileOutput, 1);	
			
			File file_output = new File(fileOutput);
			Scanner scanner_output = new Scanner(file_output);
			
			File file_output_expected = new File(fileOutput_expected);
			Scanner scanner_output_expected = new Scanner(file_output_expected);
			String line_output = "";
			String line_output_expected ="";
			while (scanner_output.hasNextLine()) {			
				line_output = scanner_output.nextLine().trim();	
				line_output_expected = scanner_output_expected.nextLine().trim();	
				assertTrue(line_output.equals(line_output_expected));
			}
			
			scanner_output.close();
			scanner_output_expected.close();
			
		}catch (LTSBTException e){
			e.printStackTrace();
		}catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	@Test
	public void testGetFailures3(){
		try{
			String path = "./test-data/failures_identifier/specification/";
			String fileInput1 = "./test-data/failures_identifier/failures_input1.txt";
			String fileOutput1 = "./test-data/failures_identifier/failures_output1.txt";
			
			String fileInput2 = "./test-data/failures_identifier/failures_input2.txt";
			String fileOutput2 = "./test-data/failures_identifier/failures_output2.txt";
			
			String fileInput3 = "./test-data/failures_identifier/failures_input3.txt";
			String fileOutput3 = "./test-data/failures_identifier/failures_output3.txt";
			
			FailuresIdentifier failuresIdentifier1 = new FailuresIdentifier();
			failuresIdentifier1.getFailuresFromAnnotatedGraph(path, fileInput1, fileOutput1, 1);	
					
			FailuresIdentifier failuresIdentifier2 = new FailuresIdentifier();
			failuresIdentifier2.getFailuresFromAnnotatedGraph(path, fileInput2, fileOutput2, 3);	
			
			FailuresIdentifier failuresIdentifier3 = new FailuresIdentifier();
			failuresIdentifier3.getFailuresFromAnnotatedGraph(path, fileInput3, fileOutput3, 3);	
			
			
		}catch (LTSBTException e){
			e.printStackTrace();
		}catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
}
